package com.gyx.lottery.common.pojo;

import com.gyx.lottery.common.errorcode.ErrorCode;
import com.gyx.lottery.common.errorcode.GlobalErrorCodeConstants;
import lombok.Data;
import org.springframework.util.Assert;

import java.io.Serializable;
@Data
public class CommonResult <T> implements Serializable {
    private Integer code;//定义业务码
    private T data;//成功时返回的数据
    private String msg;//失败信息
    public static <T> CommonResult<T> success(T data){
        CommonResult<T>result=new CommonResult<>();
        result.code= GlobalErrorCodeConstants.SUCCESS.getCode();
        result.data=data;
        result.msg="";
        return result;
    }
    public static <T> CommonResult<T> error(Integer code,String msg){
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code),"并非错误码");
        CommonResult<T>result=new CommonResult<>();
        result.code=code;
        result.msg=msg;
        return result;
    }
    public static <T> CommonResult<T>error(ErrorCode code){
       return  error(code.getCode(),code.getMsg());
    }
}
